#include "GlobalGameInstances.h"

extern Globals *GameGlobals = NULL;

TextureObj::TextureObj()
{
	ID = 0;

#ifdef _DEBUG
	FileName = "";
#endif
}

TextureObj::~TextureObj()
{

}

Globals::Globals()
{

}

Globals::~Globals()
{
	for(UINT i = 0; i < GameTextures.size(); ++i)
	{
		if(GameTextures[i])
		{
			delete GameTextures[i];
		}
	}
}

bool Globals::LoadTexturesFromList( std::string TextureListFilePath)
{
	std::fstream File(TextureListFilePath, std::fstream::in);
	char FileBuffer[MAX_CHARS_PER_LINE];
	std::string FilePathPrefix;
	std::string Filename;
	std::string FinalName;
	UINT ID = 0;

	TextureObj *TempTexObj = NULL;

	if(!File.is_open())
		return false;


	ClearCharBuffer(FileBuffer, MAX_CHARS_PER_LINE);
	File.getline(FileBuffer, MAX_CHARS_PER_LINE);

	FilePathPrefix = GetStringFromChar(FileBuffer);
	FilePathPrefix += "/";
	while(!File.eof())
	{

		ClearCharBuffer(FileBuffer, MAX_CHARS_PER_LINE);
		File.getline(FileBuffer, MAX_CHARS_PER_LINE);

		if(CheckFrontCharString(FileBuffer, MAX_CHARS_PER_LINE+1, "ID:"))
		{
			ID = atoi(&FileBuffer[3]);
		}

		ClearCharBuffer(FileBuffer, MAX_CHARS_PER_LINE);
		File.getline(FileBuffer, MAX_CHARS_PER_LINE);
		ClearCharBuffer(FileBuffer, MAX_CHARS_PER_LINE);
		File.getline(FileBuffer, MAX_CHARS_PER_LINE);

		Filename = GetStringFromChar(FileBuffer);

		TempTexObj = new TextureObj();
		FinalName = FilePathPrefix + Filename;
		TempTexObj->MainTexture.loadFromFile(FinalName);

#ifdef _DEBUG
		TempTexObj->FileName = FinalName;
#endif
		TempTexObj->ID = ID;

		GameTextures.push_back(TempTexObj);

		ClearCharBuffer(FileBuffer, MAX_CHARS_PER_LINE);
		File.getline(FileBuffer, MAX_CHARS_PER_LINE);
	}

	File.close();
	return true;

}

sf::Texture* Globals::GetTexture( UINT nID )
{
	for(UINT i = 0; i < GameTextures.size(); ++i)
	{
		if(nID == GameTextures[i]->ID)
			return &GameTextures[i]->MainTexture;
	}

	return NULL;
}

bool Globals::DoesTextureExist( UINT nID )
{
	for(UINT i = 0; i < GameTextures.size(); ++i)
	{
		if(nID == GameTextures[i]->ID)
			return true;
	}

	return false;
}